package com.tedu.openmedical.duplicate.medical_duplicate.service.impl;

import com.tedu.openmedical.base.response.CheckStatusEnum;
import com.tedu.openmedical.duplicate.copy_content.mapper.CopyContentMapper;
import com.tedu.openmedical.duplicate.copy_content.pojo.dto.ContentQuery;
import com.tedu.openmedical.duplicate.copy_content.pojo.vo.CopyContentVO;
import com.tedu.openmedical.duplicate.medical_duplicate.mapper.MedicalDuplicateMapper;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.dto.MailInfoParam;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.dto.TableSelectParam;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.dto.InsertRecordParam;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.dto.UpdateRecordParam;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.entity.MailOrder;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.entity.RecordCopy;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.vo.RecordDetailVO;
import com.tedu.openmedical.duplicate.medical_duplicate.pojo.vo.RecordTableVO;
import com.tedu.openmedical.duplicate.medical_duplicate.service.MedicalDuplicateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;

@Service
public class MedicalDuplicateServiceImpl implements MedicalDuplicateService {
    private static final Logger log = LoggerFactory.getLogger(MedicalDuplicateServiceImpl.class);
    @Autowired
    MedicalDuplicateMapper mapper;
    @Autowired
    CopyContentMapper copyContentMapper;

    @Override
    public List<RecordTableVO> loadTableData(TableSelectParam param){
        List<RecordTableVO> list = mapper.selectTableData(param);
        //设置一下状态码
        for(RecordTableVO vo:list){
            CheckStatusEnum e = CheckStatusEnum.getEnum(vo.getCheckStatusCode());
            vo.setCheckStatus(e);
        }
        //设置一下复印内容
        for(RecordTableVO vo:list){
            String content = vo.getCopyContent();
            List<Integer> contentList=new ArrayList<>();
            String contentStr = transTemp(content, contentList);
            log.debug("转换后内容：{}",contentStr);
            vo.setCopyContent(contentStr);
        }
        return list;
    }
    @Override
    public void insertDuplicate(InsertRecordParam param){
        RecordCopy record=new RecordCopy();
        BeanUtils.copyProperties(param,record);
        if(param.getId()==null){
            //新增流程
            record.setCreateTime(new Date());
            //申请来源，目前只有一个
            record.setSource(1);
            record.setCheckStatus(CheckStatusEnum.CHECKING.getCode());
            record.setCopyContent(contentToString(param.getCopyContentArr()));
            record.setCheckTime(timeToString(record.getCheckTime(),record.getCreateTime(),record.getCheckStatus()-1));
            mapper.insertRecord(record);
        }
    }

    @Override
    public void updateDuplicate(UpdateRecordParam param) {
        RecordCopy record=new RecordCopy();
        BeanUtils.copyProperties(param,record);
        if (param.getId()!=null){
            //更新流程
            //设置审核时间组
            Date updateTime = new Date();
            param.setUpdateTime(updateTime);
            //设置审核时间时注意：已经通过的流程才有审核时间
            record.setCheckTime(timeToString(param.getCheckTime(),param.getUpdateTime(),record.getCheckStatus()));
            //设置新的审核状态
            record.setCheckStatus(param.getNewCheckStatus());
            mapper.updateRecord(record);
        }
    }

    @Override
    public RecordDetailVO loadDetail(Long id) {
        RecordDetailVO vo=mapper.selectDetail(id);
        //复印用途前端自己查字典项替换显示
        //设置复印内容
        List<Integer> list=new ArrayList<>();
        String contents = transTemp(vo.getCopyContent(), list);
        vo.setCopyContent(contents);
        String[] split = contents.split(",");
        //传给前端名称数组
        vo.setCopyContentArr(split);
        //拆分复印时间，给前端传map，用于时间轴显示
        vo.setCheckTimeMap(checkTimeMap(vo.getCheckTime()));
        return vo;
    }

    @Override
    public void insertMailInfo(MailInfoParam param) {
        log.debug("插入邮寄单信息");
        MailOrder order=new MailOrder();
        BeanUtils.copyProperties(param,order);
        if(param.getApplicationId()!=null){
            mapper.insertMailOrder(order);
        }
    }

    //拆分复印时间
    private Map<String,String> checkTimeMap(String time){
        log.debug("开始处理时间轴");
        Map<String,String> timeMap=new HashMap<>();
        String[] times = time.split(",");
        log.debug("时间轴拆分完毕，开始处理");
        //针对新建申请的时间，给已提交和待审核
        if(times.length>0) {
            for (String i : times) {
                String[] split = i.split("_");
                timeMap.put(split[0], split[1]);
            }
        }
        log.debug("处理时间轴完毕，返回：{}",timeMap);
        return timeMap;
    }
    //复印内容数组转换为数据字符串
    private String contentToString(List<Integer> list){
        StringBuilder sb=new StringBuilder();
        for(int i:list){
            sb.append(i);
            sb.append(',');
        }
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
    }
    //复印内容数据字符串转换为内容字符串和数据数组
    private String transTemp(String text,List<Integer> list){
        StringBuilder purposes=new StringBuilder();
        String[] strs=text.split(",");
        //直接拿所有的content列表
        ContentQuery query=new ContentQuery();
        List<CopyContentVO> content = copyContentMapper.loadContent(query);
        for(String i:strs){
            int pId=Integer.parseInt(i);
            //放进推荐内容的数据组里
            list.add(pId);
            //跟content列表对比
            for(CopyContentVO vo:content){
                if(vo.getId()==pId){
                    purposes.append(vo.getName());
                    purposes.append(',');
                    break;
                }
            }
        }
        //删一下多的逗号
        purposes.deleteCharAt(purposes.length()-1);
        return purposes.toString();
    }
    //更新时间字符串的拼接
    private String timeToString(String checkTime,Date newTime,Integer checkStatus){
        StringBuilder time=new StringBuilder();
        if(checkTime!=null) {
            time.append(checkTime);
            time.append(",");
        }
        time.append(checkStatus);
        time.append("_");
        time.append(newTime);
        log.debug("更新时间字符串：{}",time.toString());
        return time.toString();
    }
}
